package Day17;

import java.io.*;
import java.util.*;

public class DoExpress {
    private Properties ppt = new Properties();
    private FileWriter fw = new FileWriter("C:\\Users\\刘娟\\Desktop\\express.txt", true);
    private FileReader fr = new FileReader("C:\\Users\\刘娟\\Desktop\\express.txt");
    private Random r = new Random();
    //private LinkedList<Express> ls = new LinkedList<>();

    public DoExpress() throws IOException {
    }

    //添加快递
    public boolean add(Express e) throws IOException {
        if (gui() == 0 || code() == 0) {
            return false;
        }
        e.setGui(gui());
        e.setCode(code());
        ppt.setProperty(e.getNumber(), e.toString());
        ppt.store(fw, "快递柜");
        fw.flush();
        //fw.close();
        System.out.println("取件码为：" + e.getCode());
        return true;
    }

    //删除快递
    public boolean delete(String number) throws IOException {
        Express e = findNumber(number);
        if (e == null) {
            return false;
        } else {
            ppt.remove(e.getNumber());
            FileWriter fw = new FileWriter("C:\\Users\\刘娟\\Desktop\\express.txt");
            ppt.store(fw,"快递柜");
            fw.flush();
            fw.close();
            return true;
        }
    }

    //查找快递
    public Express findNumber(String number) {
        Set<String> set = ppt.stringPropertyNames();
        for (String key : set) {
            if (key.equals(number)) {
                String str = (String) ppt.get(key);
                int a = str.indexOf("{");
                int b = str.indexOf("}");
                String str1 = str.substring(a + 1, b);
                Express e = null;
                String[] str2 = str1.split(",");
                for (String str3 : str2) {
                    String[] str4 = str3.split("=");
                    String danhao = str4[1];
                    String company = str4[1];
                    int code = Integer.parseInt(str4[1]);
                    int gui = Integer.parseInt(str4[1]);
                    e = new Express(danhao, company, code, gui);
                    return e;
                }
            }
        }
        return null;
    }

    //随机生成取件码
    public int code() {
        int d = r.nextInt(90000) + 10000;
        Set<String> set = ppt.stringPropertyNames();
        for (String key : set) {
            String str = (String) ppt.get(key);
            int a = str.indexOf("{");
            int b = str.indexOf("}");
            String str1 = str.substring(a + 1, b);
            Express e = null;
            String[] str2 = str1.split(",");
            for (String str3 : str2) {
                String[] str4 = str3.split("=");
                String danhao = str4[1];
                String company = str4[1];
                int c = Integer.parseInt(str4[1]);
                int gui = Integer.parseInt(str4[1]);
                e = new Express(danhao, company, c, gui);
                if (e.getCode() == a) {
                    return 0;
                }
            }
        }
        return d;
    }



    //随机生成快递柜
    public int gui() {
        int d = r.nextInt(90) + 10;
        Set<String> set = ppt.stringPropertyNames();
        for (String key : set) {
            String str = (String) ppt.get(key);
            int a = str.indexOf("{");
            int b = str.indexOf("}");
            String str1 = str.substring(a + 1, b);
            Express e = null;
            String[] str2 = str1.split(",");
            for (String str3 : str2) {
                String[] str4 = str3.split("=");
                String danhao = str4[1];
                String company = str4[1];
                int c = Integer.parseInt(str4[1]);
                int gui = Integer.parseInt(str4[1]);
                e = new Express(danhao, company, c, gui);
                if (e.getGui() == a) {
                    return 0;
                }
            }
        }
        return d;
    }

    //修改快递
    public boolean revise(Express e) throws IOException {
        View v = new View();
        Express e1 = findNumber(v.findByNumber());
        if(e1 == null){
            return false;
        }else {
            ppt.remove(e1.getNumber());
            FileWriter fw = new FileWriter("C:\\Users\\刘娟\\Desktop\\express.txt");
            ppt.store(fw,"快递柜");
            fw.flush();
            fw.close();
            add(e);
            return true;
        }
    }

    //遍历快递
    public void printAll() {
        try {
            ppt.load(fr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Enumeration fileName = ppt.propertyNames();
        while (fileName.hasMoreElements()) {
            String strKey = (String) fileName.nextElement();
            String strValue = ppt.getProperty(strKey);
            System.out.println(strKey + "->" + strValue);
        }
    }

    public boolean user(int code) throws IOException {
        Set<String> set = ppt.stringPropertyNames();
        for (String key : set) {
            String str = (String) ppt.get(key);
            int a = str.indexOf("{");
            int b = str.indexOf("}");
            String str1 = str.substring(a + 1, b);
            Express e = null;
            String[] str2 = str1.split(",");
            for (String str3 : str2) {
                String[] str4 = str3.split("=");
                String danhao = str4[1];
                String company = str4[1];
                int c = Integer.parseInt(str4[1]);
                int gui = Integer.parseInt(str4[1]);
                e = new Express(danhao, company, c, gui);
                if(e.getCode() == code){
//                    ppt.remove(e.getNumber());
//                    FileWriter fw = new FileWriter("C:\\Users\\刘娟\\Desktop\\express.properties");
//                    ppt.store(fw,"快递柜");
//                    fw.flush();
//                    fw.close();
                    delete(e.getNumber());
                    return true;
                }
            }
        }
        return false;
    }
}
